package question12;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;

public class Main {

    public static int countStudents(int[] students, int[] sandwiches) {
        List<Integer> student = new ArrayList<>();
        Stack<Integer> stack = new Stack<>();
        for(int i = 0; i < students.length; i++) {
            student.add(students[i]);
        }
        for(int i = sandwiches.length - 1; i >= 0; i--) {
            stack.push(sandwiches[i]);
        }
        return getNum(stack, student);
    }

    public static int getNum(Stack<Integer> stack, List<Integer> student) {
        while(true) {
            Iterator<Integer> iterator = student.iterator();
            boolean  hasTake = false;
            while(iterator.hasNext()) {
                Integer value = iterator.next();
                if (value.equals(stack.peek())) {
                    hasTake = true;
                    stack.pop();
                    iterator.remove();
                }
            }
            if(!hasTake) {
                return student.size();
            }
        }
        // return students.size();
    }

    public static void main(String[] args) {
        int[] students = {1,1,1,0,0,1};
        int[] sandwiches = {1,0,0,0,1,1};
        System.out.println(countStudents(students, sandwiches));
    }
}
